package es.ats.codington.festival.services;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import es.ats.codington.festival.daos.ManagerDAO;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.interfaces.daos.IEventDAO;
import es.ats.codington.festival.interfaces.daos.IVisitorDAO;
import es.ats.codington.festival.interfaces.services.IEventService;
/**
 * 
 * @author Codington_RED
 *
 */

public class EventService implements IEventService{

	/**
	 * method that gets a list of events from database
	 * @return allEvents
	 * @throws ServiceException
	 */
	@Override
    public List<Event> getAllEvents() throws ServiceException {	
	
		ManagerDAO manager = null;
		IEventDAO eventDAO = null;
		List<Event> allEvents = new ArrayList<Event>();	
			
		try {	    	  
		    manager = new ManagerDAO();
		    eventDAO = manager.getEventDAO();
		    
		    allEvents = eventDAO.showAllEvents();
		    
		    manager.closeConnection();
		    
		} catch(DAOException ex) {	
		    if(manager != null) {
		    	
		    	manager.closeError();
			
		    }
	    
		    throw new ServiceException("Error accessing database", ex);
	    
		}
		
		return allEvents;
    }
	
	/**
	 * method that gets a list of events from database using the EventName
	 * @param eventName
	 * @return allEvents
	 * @throws ServiceException
	 */
    @Override
    public List<Event> getAllEvents(Event eventName) throws ServiceException {	
	
		ManagerDAO manager = null;
		IEventDAO eventDAO = null;
		List<Event> allEvents = new ArrayList<Event>();	
			
		try {
		    manager = new ManagerDAO();
		    eventDAO = manager.getEventDAO();
		    
		    allEvents = eventDAO.showEvents(eventName);
		    
		    manager.closeConnection();
		    
		} catch(DAOException ex) {
		    if(manager != null) {
			manager.closeError();
			
		    }
		    
		    throw new ServiceException("Error accessing database", ex);
		    
		}
			
		return allEvents;		
    }

    /**
     * method that checks if does exits an event
     * @param event
     * @return eventhere
     * @throws ServiceException
     * @throws SQLException
     */
    @Override
    public boolean checkEvent(Event event) throws ServiceException, SQLException {
		boolean eventhere = false;
		ManagerDAO manager = null;
		IEventDAO eventDAO = null;
		
		try {
			manager = new ManagerDAO();
			eventDAO = manager.getEventDAO();
			 eventhere = eventDAO.getCheckEvent(event);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return eventhere;
	}
    
    /**
     * method that obtains an event taking the event id
     * @param eventId
     * @return event
     * @throws ServiceException 
     */
    @Override
    public Event getEvent(int eventId) throws ServiceException {
	
		ManagerDAO manager = null;
		Event event = null;	
		IEventDAO eventDAO = null;
		
		try {
		    manager = new ManagerDAO();
		    eventDAO = manager.getEventDAO();
		    
		    event = eventDAO.getEvent(eventId);
		    
		    manager.closeConnection();
		    
		} catch(DAOException ex) {
		    if(manager != null) {
			manager.closeError();
			
		    }
		    
		    throw new ServiceException("Error accessing database", ex);
		    
		}	
		
		return event;	
	    }

    /**
     * method that obtains the eventId
     * @param event
     * @return id
     * @throws ServiceException
     * @throws SQLException
     */
    @Override
    public int getIdEvent(Event event) throws ServiceException, SQLException {
    	ManagerDAO manager = null;
		IEventDAO eventDAO = null;
		int id = 0;
			
		try {
		    manager = new ManagerDAO();
		    eventDAO = manager.getEventDAO();
		    
		    id = eventDAO.getIdEvent(event);
		    
		} catch(DAOException ex) {	    
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {		
			if(id == 1) {
			    manager.closeConnection();
			    
			} else {
			    manager.closeError();
			    
			}		
		    }	    
		}
				
		return id;
    }
    
    /**
     * method that modifies all the information relative to an event
     * @param event
     * @return affectedRows
     * @throws ServiceException 
     */
    @Override
    public int updateEvent(Event event) throws ServiceException {
	
		ManagerDAO manager = null;
		IEventDAO eventDAO = null;
		Integer affectedRows = 0;
			
		try {
		    manager = new ManagerDAO();
		    eventDAO = manager.getEventDAO();
		    
		    affectedRows = eventDAO.updateEvent(event);
		    
		} catch(DAOException ex) {	    
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {		
			if(affectedRows == 1) {
			    manager.closeConnection();
			    
			} else {
			    manager.closeError();
			    
			}		
		    }	    
		}
				
		return affectedRows;
    }

    /*@Override
    public int deleteEvent(int eventId) throws ServiceException {
	
	DAOManager manager = null;
	IEventDAO eventDAO = null;
	IEventSignUpDAO eventSingUpDAO = null;
	Integer affectedRows = 0;
		
	try {	   
	    manager = new DAOManager();
	    eventDAO = manager.getEventDAO();	 
	    eventSingUpDAO = manager.getEventSignUpDAO();
	    
	    eventSingUpDAO.deleteEventSignUp(eventId);
	    affectedRows = eventDAO.deleteEvent(eventId);
	    
	} catch(DAOException ex) {	    
	    throw new ServiceException("Error de acceso a la base de datos.", ex);
	    
	} finally {
	    if(manager != null) {
		if(affectedRows == 1) {
		    manager.closeOK();
		    
		} else {
		    manager.closeError();
		    
		}		
	    }	    
	}	
	
	return affectedRows;
    }*/

    
    /**
     * method that inserts an event into the database
     * @param event
     * @return affectedRows
     * @throws ServiceException
     */
    @Override
    public boolean insertEvent(Event event) throws ServiceException{
	
    	ManagerDAO manager = null;	
		IEventDAO eventDAO = null;
		//ISequencerDAO sequencerDAO = null;
		boolean affectedRows = false;
		//Integer eventId = 0;
		
		try {
		    manager = new ManagerDAO();
		    eventDAO = manager.getEventDAO();
		    //sequencerDAO = manager.getSequencerDAO();
		    	    
		    //eventId = sequencerDAO.getNext(ISequencerDAO.CLASS_EVENT_NAME);
		    //event.setEventId(eventId);
		    
		    affectedRows = eventDAO.insertEvent(event);
		   
		    
		} catch(DAOException ex) {	    
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {
		    	if(affectedRows == true) {
		    		manager.closeConnection();
			    
		    	} else {
		    		manager.closeError();
			    }		
		    }
		}
			
		return affectedRows;
    }
    
    
    /**
     * method that updates the number of seats available for the event
     * @param event
     * @return affectedRows
     * @throws ServiceException
     */
    @Override
    public int updateSeatsEventDec(Event event) throws ServiceException {
    	ManagerDAO manager = null;
		IEventDAO eventDAO = null;
		Integer affectedRows = 0;
			
		try {
		    manager = new ManagerDAO();
		    eventDAO = manager.getEventDAO();
		    
		    affectedRows = eventDAO.updateSeatsEventDec(event);
		    
		} catch(DAOException ex) {	    
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {		
			if(affectedRows == 1) {
			    manager.closeConnection();
			    
			} else {
			    manager.closeError();
			    
			}		
		    }	    
		}
				
		return affectedRows;
    }
    
    
    /**
     * method that updates the number of seats available for the event
     * @param event
     * @return affectedRows
     * @throws ServiceException
     */
    @Override
    public int updateSeatsEventInc(Event event) throws ServiceException {
    	ManagerDAO manager = null;
		IEventDAO eventDAO = null;
		Integer affectedRows = 0;
			
		try {
		    manager = new ManagerDAO();
		    eventDAO = manager.getEventDAO();
		    
		    affectedRows = eventDAO.updateSeatsEventInc(event);
		    
		} catch(DAOException ex) {	    
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {		
			if(affectedRows == 1) {
			    manager.closeConnection();
			    
			} else {
			    manager.closeError();
			    
			}		
		    }	    
		}
				
		return affectedRows;
    }

}
